package com.example.adminserivce.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adminserivce.Service.TeacherService;
import com.example.common.domain.Result;
import com.example.common.domain.dto.search.TeacherSearchDto;
import com.example.common.domain.pojo.Teacher;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/admins")
@RequiredArgsConstructor
@Api(value = "管理员端接口", description = "老师信息管理")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 根据id查询老师信息
     *
     * @param id 要查询的老师id
     * @return 老师信息
     */
    @GetMapping("/getTeacherById/{id}")
    public Result<Teacher> getTeacherById(@PathVariable String id) {
        return Result.success(teacherService.getTeacherByIdWithLogicalExpire(id));
    }

    /**
     * 分页条件查询老师信息
     *
     * @param searchDto 查询条件
     * @return 老师信息（分页）
     */
    @GetMapping("/getTeacher")
    public Result<Page<Teacher>> getStudents(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            TeacherSearchDto searchDto) {

        Page<Teacher> page = new Page<>(pageNum, pageSize);
        return Result.success(teacherService.pageByCriteria(page, searchDto));
    }

    /**
     * 添加老师信息
     *
     * @param teacher 要添加的老师信息
     * @return 添加结果
     */
    @PostMapping("/addTeacher")
    public Result<Boolean> addTeacher(@RequestBody Teacher teacher) {
        return Result.success(teacherService.save(teacher));
    }

    /**
     * 删除老师信息
     *
     * @param id 要删除的老师id
     *           状态删除
     */
    @DeleteMapping("/deleteTeacher")
    public Result<Boolean> deleteTeacher(@RequestParam String id) {
        return Result.success(teacherService.removeById(id));
    }

    /**
     * 修改老师信息
     *
     * @param teacher 要修改的老师信息
     * @return 修改结果
     */
    @PostMapping("/updateTeacher")
    public Result<Boolean> updateTeacher(@RequestBody Teacher teacher) {
        return Result.success(teacherService.updateById(teacher));
    }

    /**
     * 批量添加老师信息
     *
     * @param teachers 要添加的老师信息
     * @return 添加结果
     */
    @PostMapping("/addTeachers")
    public Result<Boolean> addTeachers(@RequestBody Teacher[] teachers) {
        return Result.success(teacherService.saveBatch(Arrays.asList(teachers)));
    }

    /**
     * 批量删除老师信息
     *
     * @param ids 要删除的老师id
     * @return 删除结果
     */
    @DeleteMapping("/deleteTeachers")
    public Result<Boolean> deleteTeachers(@RequestParam String[] ids) {
        return Result.success(teacherService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 批量修改老师信息
     *
     * @param teachers 要修改的老师信息
     * @return 修改结果
     */
    @PostMapping("/updateTeachers")
    public Result<Boolean> updateTeachers(@RequestBody Teacher[] teachers) {
        return Result.success(teacherService.updateBatchById(Arrays.asList(teachers)));
    }


}

